package com.itheima.paging;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Test01 {
    @Test
    public void selectPaging() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = build.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //通过分页助手来实现分页功能
        // 第一页：显示三条数据
//        PageHelper.startPage(1,3);
        // 第二页：显示三条数据
//        PageHelper.startPage(2,3);
        // 第三页：显示三条数据
        // startPage()的参数有两个，第一个是当前的页数，第二个是当前页数据的条数
        PageHelper.startPage(3,3);

        List<Student> list = mapper.selectAll();

        for (Student student1 : list) {
            System.out.println(student1);
        }

        // 获取分页相关的参数
        PageInfo<Student> info = new PageInfo<>(list);
        System.out.println("总条数："+info.getTotal());
        System.out.println("总页数："+info.getPages());
        System.out.println("当前："+info.getPageNum());
        System.out.println("每页显示的条数："+info.getPageSize());
        System.out.println("上一页："+info.getPrePage());
        System.out.println("下一页："+info.getNextPage());
        System.out.println("是否是第一页："+info.isIsFirstPage());
        System.out.println("是否是最后一页："+info.isIsLastPage());


        sqlSession.close();
        is.close();

    }
}
